package fr.uniscol.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import fr.uniscol.dao.IGeographieDao;
import fr.uniscol.model.geo.Academie;
import fr.uniscol.model.geo.Commune;
import fr.uniscol.model.geo.Departement;
import fr.uniscol.model.geo.Pays;

public class GeographieDaoImpl implements IGeographieDao {

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Academie> getAllAcademies() {
		
		Session session = sessionFactory.getCurrentSession();
		
		List<Academie> academies = new ArrayList<Academie>();
		
		Query q = session.createQuery("From Academie");
		
		return q.list();
	}

	public Academie getAcademie(int code) {
		
		Session session = sessionFactory.getCurrentSession();
		
		Academie aca = (Academie) session.get(Academie.class, code);
		
		return aca;
	}

	public List<Departement> getAllDepartements() {
		
		Session session = sessionFactory.getCurrentSession();
		
		Query q = session.createQuery("From Departement");
		
		return q.list();
	}

	public List<Pays> getAllPays() {
		
		Session session = sessionFactory.getCurrentSession();
		
		Query q = session.createQuery("From Pays");
		
		return q.list();
	}

	public List<Commune> getAllCommunes() {
		
		Session session = sessionFactory.getCurrentSession();
		
		Query q = session.createQuery("From Commune");
		
		return q.list();
	}

	public Departement getDepartement(String code) {
		
		Session session = sessionFactory.getCurrentSession();
		
		Departement dep = (Departement) session.get(Departement.class, code);
		
		return dep;
	}

	public Pays getPays(int code) {
		
		Session session = sessionFactory.getCurrentSession();
		
		Pays p = (Pays) session.get(Pays.class, code);
		
		return p;
	}

	public Commune getCommune(int code) {
		
		Session session = sessionFactory.getCurrentSession();
		
		Commune com = (Commune) session.get(Commune.class, code);
		
		return com;
	}

	public void enregistrerPays(Pays pays) {
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(pays);
	}

	public void enregistrerDepartement(Departement departement) {
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(departement);
	}

	public void enregistrerCommune(Commune commune) {
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(commune);
	}

}
